<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template  slot="menuLeft">
        <el-button  type="warning" icon="el-icon-back" size="small" @click.stop="goBack()">返回列表</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/tjkjkcjobrecords";
  import {mapGetters} from "vuex";

  export default {
    props: {
      jobId: {
        type: String,
        default: '0'
      },
      xsId: {
        type: String,
        default: '0'
      },
      showXsMainView: {
        type: Function,
        default: null
      },
      showJobMainView: {
        type: Function,
        default: null
      }
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide: true,
              display:false
            },
            {
              label: "教师",
              prop: "jsId",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入教师ID",
                trigger: "blur"
              }],
              type: "select",
              dicUrl: "/api/school-service/js/select",
              props: {
                label: "jsName",
                value: "id"
              },
              editDisabled:true
            },
            // {
            //   label: "任务ID",
            //   prop: "jobId",
            //   rules: [{
            //     required: true,
            //     message: "请输入任务ID",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "学生ID",
            //   prop: "xsId",
            //   rules: [{
            //     required: true,
            //     message: "请输入学生ID",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "时间",
              prop: "recordTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入时间",
                trigger: "blur"
              }],
              editDisabled:true
            },
            {
              label: "是否公开",
              prop: "isPublic",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入是否公开",
                trigger: "blur"
              }],
              type: "select",
              dicData: [

                {
                  label: "是",
                  value: 1
                },
                {
                  label: "否",
                  value: 0
                }
              ],
              dataType:'number',
              editDisabled:true
            },
            {
              label: "地点",
              prop: "recordAddr",
              rules: [{
                required: true,
                message: "请输入地点",
                trigger: "blur"
              }],
              hide:true,
              maxlength:200,
              showWordLimit:true,
              editDisabled:true
            },
            {
              label: "描述",
              prop: "recordDesc",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入描述",
                trigger: "blur"
              }],
              hide:true
            },

            {
              label: "图1",
              prop: "pic1",
              rules: [{
                required: true,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "图2",
              prop: "pic2",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "图3",
              prop: "pic3",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "图4",
              prop: "pic4",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "图5",
              prop: "pic5",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },
            {
              label: "图6",
              prop: "pic6",
              rules: [{
                required: false,
                message: "请输入图1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true,
              editDisabled:true
            },

            {
              label: "回复内容",
              prop: "jsAnswer",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: false,
                message: "请输入教师回复内容",
                trigger: "blur"
              }],
              hide:true,
              maxlength:200,
              showWordLimit:true

            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: true,
          viewBtn: true,
          delBtn: this.vaildData(this.permission.tjkjkcjobrecords_delete, false),
          editBtn: true
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      goBack(){
        if(this.jobId&&this.jobId!=='0')
          this.showJobMainView();
        else this.showXsMainView();
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.xsId = this.xsId;
        params.jobId = this.jobId;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
